package indicator

import (
	"quant/backend/constants"

	"github.com/duke-git/lancet/v2/mathutil"
)

type MA struct {
	n      int     // 5代表 MA5(5日均线)，类推10、20 等
	result float64 // 5日均线值
}

func NewMA(n int) *MA {
	return &MA{
		n: n,
	}
}

func (ma *MA) Update(prices []float64) float64 {
	if len(prices) <= 0 {
		ma.result = 0
	} else {
		ma.result = mathutil.Sum(prices[0:mathutil.Min(ma.n, len(prices))]...) / float64(ma.n)
	}
	return mathutil.RoundToFloat(ma.result, constants.Precision6)
}
